package com.xinyue.ext.reader.bean;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;

import com.xinyue.ext.data.FieldData;
import com.xinyue.ext.exception.BeanException;
import com.xinyue.ext.reader.ParamHolder;

public class IdProcess extends BeanContext{
	//私有构造器
	private IdProcess() {
	}
	//私有内部类
	private static class SingletonHolder{
        private static IdProcess instance = new IdProcess();  
    }
	/***
	 * 获得实例对象
	 */
	public static IdProcess getInstance(ParamHolder holder) {
		BeanContext.holder = holder;
		return SingletonHolder.instance;
	}
	
	
	/**
	 * 读取Id标签中的信息
	 * param e entity
	 */
	@Override
	public void read(Element e) throws BeanException {
		Element id = e.element(id_);
		if(id == null) {
			throw new BeanException("id标签未定义", holder.getException());
		}
		
		//公共属性
		FieldData field = commonRead(id);
		
		//注解
		List<String> ann_id = new ArrayList<String>();
		ann_id.add("@Id");
		holder.getBeanImpt().add("javax.persistence.Id");
		//主键生成方式
		Element gv = id.element("generatedValue");
		if(gv != null) {
			ann_id.add("@GeneratedValue(strategy=GenerationType." + gv.attributeValue(value_) + ")");
		}else {
			field.setAuto_key(true);
			ann_id.add("@GeneratedValue(strategy=GenerationType.IDENTITY)");
		}
		holder.getBeanImpt().add("javax.persistence.GeneratedValue");
		holder.getBeanImpt().add("javax.persistence.GenerationType");
		
		field.setAnn_id(ann_id);
		holder.getEntity().setId(field);
	}
}
